package com.pengzy.supports.dto;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 封装权限信息
 * @author xiaozhi
 */
@Data
@NoArgsConstructor
public class LoginUser implements UserDetails {
    /**
     * // 权限
     * 	// 用户的权限集， 默认需要添加ROLE_ 前缀
     *     Collection<? extends GrantedAuthority> getAuthorities();
     * 	// 密码
     * 	// 用户的加密后的密码， 不加密会使用{noop}前缀
     *     String getPassword();
     * 	// 用户名
     *     String getUsername();
     * 	// 帐户未过期
     *     boolean isAccountNonExpired();
     *     // 帐户未锁定
     *     boolean isAccountNonLocked();
     * 	// 凭证是否过期
     *     boolean isCredentialsNonExpired();
     * 	// 用户是否可用
     *     boolean isEnabled();
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private UserDetail user;

    private String username;

    @Override
    public String toString() {
        return "LoginUser{" +
                "user=" + user +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", token='" + token + '\'' +
                ", permissions=" + permissions +
                ", authorities=" + authorities +
                '}';
    }

    @JsonIgnore
    private String password;

    private String token;

    private String roles;


    //存储权限信息
    private List<String> permissions;

    //存储SpringSecurity所需要的权限信息的集合
    @JSONField(serialize = false)
    @JsonIgnore
    private List<GrantedAuthority> authorities;


    public LoginUser(UserDetail user, List<String> permissions, String username, String password){
        this.user=user;
        this.permissions=permissions;
        this.username=username;
        this.password=password;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if(permissions==null){
            return null;
        }
        //把permissions中字符串类型的权限信息转换成GrantedAuthority对象存入authorities中
        authorities = permissions.stream().
                map(SimpleGrantedAuthority::new)
                .collect(Collectors.toList());
        return authorities;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isEnabled() {
        return true;
    }
}
